//
//  ByFootBallScoreBetPopView.m
//  caipiao_ios
//
//  Created by mc on 2018/7/23.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByFootBallScoreBetPopView.h"
#import "BySureCancelView.h"



#define ViewWidth1  (SCREEN_WIDTH - 20)
#define WIDTH1 (ViewWidth1 - 20)

@implementation ByFootBallScoreBetPopView

- (id)init{
    self = [super init];
    if (self) {
        UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        backgroundView.backgroundColor = rgba(120, 120, 120, 0.3);
        self.backgroundView = backgroundView;
        [[UIApplication sharedApplication].keyWindow addSubview:backgroundView];
        
        
        CGFloat H = 46 * 7 + 50 + 10 + 60;
        self.frame = CGRectMake(10, (SCREEN_HEIGHT - H) / 2.0, ViewWidth1, H);
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 5;
        self.backgroundColor = BY_BACKGROUND_COLOR;
        
        
        self.scoresView = [[UIView alloc] initWithFrame:CGRectMake(10, 50 + 10, WIDTH1, 46 * 7)];
        [self addSubview:self.scoresView];
        self.scoresView.backgroundColor = BY_LINE_COLOR;
        self.scoresView.layer.cornerRadius = 5;
        self.scoresView.layer.masksToBounds = YES;
    
        
        
        BySureCancelView *sureCancelView = [[BySureCancelView alloc] init];
        [self addSubview:sureCancelView];
        [sureCancelView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(0);
            make.right.equalTo(self).offset(0);
            make.bottom.equalTo(self).offset(0);
            make.height.mas_equalTo(@60);
        }];
        sureCancelView.cancelBlock = ^(){
            [self removeViews];
            if (self.selectedBlock) {
                self.selectedBlock(self.scoresArray1);
            }
            
        };
        sureCancelView.sureBlock = ^(){
            if (self.selectedBlock) {
                self.selectedBlock(self.scoresArray);
            }
            [self removeViews];
        };
        [self initTitleViews];
        
        [[UIApplication sharedApplication].keyWindow addSubview:self];
    }
    return self;
}



- (void)setScoresArray:(NSMutableArray *)scoresArray{
    _scoresArray = scoresArray;
    ByNetModel *titleModel = [_scoresArray firstObject];
    self.bottomView.netModel = titleModel;
    
    self.scoresArray1 = [NSMutableArray arrayWithCapacity:1];
    for (ByNetModel *netModel in scoresArray) {
        ByNetModel *model = [netModel copy];
        [self.scoresArray1 addObject:model];
    }
    
    [self initPointsViews];
}


- (void)setModel:(ByNetModel *)model WithOtherModel:(ByNetModel *)netModel{
    
    
}


- (void)initTitleViews{
    ByTootBallPopTitleView *bottomView = [[ByTootBallPopTitleView alloc] init];
    [self addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(0);
        make.top.equalTo(self).offset(0);
        make.right.equalTo(self).offset(0);
        make.height.mas_equalTo(@50);;
    }];
    self.bottomView = bottomView;
}


- (void)initPointsViews{
    [self.scoresView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    UILabel *pointLabel1 = [self createLabelWithFrame:CGRectMake(1, 1, 20, 46 * 3 - 2) backgroundColor:rgba(255, 219, 217, 1) text:@"胜" textColor:BY_TEXT_COLOR font:12];
    pointLabel1.numberOfLines = 0;
    [self.scoresView addSubview:pointLabel1];
    
    UILabel *pointLabel2 = [self createLabelWithFrame:CGRectMake(1, pointLabel1.bottom + 1, 20, 46 - 2) backgroundColor:rgba(198, 231, 255, 1) text:@"平" textColor:BY_TEXT_COLOR font:12];
    pointLabel2.numberOfLines = 0;
    [self.scoresView addSubview:pointLabel2];
    
    UILabel *pointLabel3 = [self createLabelWithFrame:CGRectMake(1, pointLabel2.bottom + 1, 20, 46 * 3 - 2) backgroundColor:rgba(190, 224, 94, 1) text:@"负" textColor:BY_TEXT_COLOR font:12];
    pointLabel3.numberOfLines = 0;
    [self.scoresView addSubview:pointLabel3];
    
    
    CGFloat left = 1;
    CGFloat magin = 1;
    CGFloat width = (WIDTH1 - 21 - left * 6) / 5.0;
    CGFloat height = 45;
    
    for (int i = 0; i < 13; i++) {
        ByNetModel *netModel = [self.scoresArray objectAtIndex:i];
        UILabel *valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(22 + i % 5 * (width + magin),1 + i / 5 * (height + magin), width,height)];
        [self.scoresView addSubview:valueLabel];
       
        valueLabel.tag = 400 + i;
        valueLabel.font = FONT(12);
        valueLabel.numberOfLines = 0;
        valueLabel.textAlignment = NSTextAlignmentCenter;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scoresTapAction:)];
        [valueLabel addGestureRecognizer:tap];
        
        if (netModel.isNoSale) {
            valueLabel.text = @"未开售";
            valueLabel.userInteractionEnabled = NO;
        }else{
           valueLabel.text = [NSString stringWithFormat:@"%@\n%@", netModel.title,netModel.valueString];
            valueLabel.userInteractionEnabled = YES;
        }
        if (i == 12) {
            [valueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(self.scoresView).offset(-1);
                make.top.equalTo(self.scoresView).offset(3 + 45 * 2);
                make.size.mas_equalTo(CGSizeMake(width * 3 + 2, height));
            }];
        }
        

        if (netModel.selected) {
            valueLabel.backgroundColor = rgba(255, 148, 87, 1);
            valueLabel.textColor = [UIColor whiteColor];
        }else{
            
            valueLabel.textColor = [UIColor lightGrayColor];
            valueLabel.backgroundColor = [UIColor whiteColor];
            
        }
        
    }
    
    
    for (int i = 0; i < self.scoresArray.count - 13; i++) {
        
        ByNetModel *netModel = [self.scoresArray objectAtIndex:i + 13];
        UILabel *valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(22 + i % 5 * (width + magin),height * 3 + 4 + i / 5 * (height + magin), width,height)];
        [self.scoresView addSubview:valueLabel];

        valueLabel.tag = 400 + i + 13;
        valueLabel.font = FONT(14);
        valueLabel.numberOfLines = 0;
        valueLabel.textAlignment = NSTextAlignmentCenter;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scoresTapAction:)];
        [valueLabel addGestureRecognizer:tap];
        if (netModel.isNoSale) {
            valueLabel.text = @"未开售";
            valueLabel.userInteractionEnabled = NO;
        }else{
            valueLabel.text = [NSString stringWithFormat:@"%@\n%@", netModel.title,netModel.valueString];
            valueLabel.userInteractionEnabled = YES;
        }
        
        
        
        if (netModel.selected) {
            valueLabel.backgroundColor = rgba(255, 148, 87, 1);
            valueLabel.textColor = [UIColor whiteColor];
        }else{
            
            valueLabel.textColor = [UIColor lightGrayColor];
            valueLabel.backgroundColor = [UIColor whiteColor];
            
        }
        
    }
    
}

- (void)scoresTapAction:(UITapGestureRecognizer *)tap{
    NSInteger index = tap.view.tag - 400;
    ByNetModel *netModel = [self.scoresArray objectAtIndex:index];
    netModel.selected = !netModel.selected;
    [self initPointsViews];
    
}



- (void)removeViews{
    
    [UIView animateWithDuration:0.5 animations:^{
        self.frame = CGRectMake(self.frame.origin.x-SCREEN_WIDTH, self.frame.origin.y,self.frame.size.width, self.frame.size.height);
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self.backgroundView removeFromSuperview];
        
        [self removeFromSuperview];
    });
}

- (UILabel *)createLabelWithFrame:(CGRect)frame backgroundColor:(UIColor *)backColor text:(NSString *)text textColor:(UIColor *)textColor font:(int)font{
    
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    if (backColor) {
        label.backgroundColor = backColor;
    }else{
        label.backgroundColor = [UIColor clearColor];
    }
    label.text = text;
    label.textColor = textColor;
    label.font = [UIFont systemFontOfSize:font];
    label.textAlignment = NSTextAlignmentCenter;
    return label;
}

@end
